/*
 * @(#)GenerationParametersScreen.java     14/08/02
 * 
 * Author             Rick Wilson
 * Copyright (c) 2014 Rick Wilson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of 'Taareekah' nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */



package gui;

//~--- non-JDK imports --------------------------------------------------------

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ButtonClickedEvent;
import de.lessvoid.nifty.controls.ListBox;
import de.lessvoid.nifty.controls.ListBoxSelectionChangedEvent;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.controls.TextFieldChangedEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

import parameters.Parameters;
import parameters.ParameterGroup;

//~--- JDK imports ------------------------------------------------------------

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author rick
 */
public class GenerationParametersScreen implements ScreenController {

    /** Field description */
    private Nifty	nifty;

    /** Field description */
    private Screen	screen;

    /** Field description */
    private ListBox	list_box;

    /** Field description */
    private TextField	min_textfield;

    /** Field description */
    private TextField	max_textfield;

    /** Field description */
    private TextField	mesh_textfield;

    /** Field description */
    private TextField	save_filename_textfield;

    /** Field description */
    private TextField	world_name_textfield;

    /** Field description */
    private TextField	size_textfield;

    /** Field description */
    private TextField	seed_textfield;

    /** Field description */
    private String	selection;

    /** Field description */
    private ParameterGroup	selected_group;

    /** Field description */
    private ParameterGroup	root;

    /**
     * Method description
     *
     *
     * @return
     *
     * @throws Exception
     */
    private float getSelectedMin() throws Exception {
	return selected_group.getChild("min").getValueAsFloat();
    }

    /**
     * Method description
     *
     *
     * @return
     *
     * @throws Exception
     */
    private float getSelectedMax() throws Exception {
	return selected_group.getChild("max").getValueAsFloat();
    }

    /**
     * Method description
     *
     *
     * @return
     *
     * @throws Exception
     */
    private int getSelectedMesh() throws Exception {
	return selected_group.getChild("mesh").getValueAsInt();
    }

    /**
     * Method description
     *
     *
     * @param value
     */
    private void setSelectedGroup(String value) {
	try {
	    ParameterGroup	p	= null;

	    switch (selection) {
	    case "Elevation" :
		selected_group	= (ParameterGroup) Parameters.getParameterSet("general").getParameterNode(
		    "terrain.elevation");

		break;

	    case "Temperature" :
		selected_group	= (ParameterGroup) Parameters.getParameterSet("general").getParameterNode(
		    "terrain.temperature");

		break;

	    case "Rainfall" :
		selected_group	= (ParameterGroup) Parameters.getParameterSet("general").getParameterNode(
		    "terrain.rainfall");

		break;

	    case "Drainage" :
		selected_group	= (ParameterGroup) Parameters.getParameterSet("general").getParameterNode(
		    "terrain.drainage");

		break;

	    case "Volcanism" :
		selected_group	= (ParameterGroup) Parameters.getParameterSet("general").getParameterNode(
		    "terrain.volcanism");

		break;
	    }
	} catch (Exception ex) {
	    Logger.getLogger(GenerationParametersScreen.class.getName()).log(Level.SEVERE,
			     "Couldnt find the parameter set for {0} {1}", new Object[] { value,
		    ex.toString() });
	}
    }

    /**
     * Method description
     *
     *
     * @throws Exception
     */
    private void reset() throws Exception {
	try {
	    selection		= "";
	    selected_group	= null;
	    root		= (ParameterGroup) Parameters.getParameterSet("general").getParameterNode("");
	    world_name_textfield.setText(root.getChild("worldname").getValueAsString());
	    size_textfield.setText(root.getChild("size").getValueAsString());
	    seed_textfield.setText(root.getChild("seed").getValueAsString());
	    min_textfield.setText("");
	    max_textfield.setText("");
	    mesh_textfield.setText("");
	} catch (Exception ex) {
	    Logger.getLogger(GenerationParametersScreen.class.getName()).log(Level.SEVERE, "reset failed {0}",
			     ex.toString());

	    throw ex;
	}
    }

    /**
     *
     * @param id
     * @param event
     */
    @NiftyEventSubscriber(pattern = "parameters_[\\w]+?_textfield")
    public void onTextChanged(final String id, TextFieldChangedEvent event) {
	try {
	    switch (id) {
	    case "parameters_min_textfield" :
		if (selected_group != null) {
		    selected_group.getChild("min").setValue(Float.parseFloat(event.getText()));
		}

		break;

	    case "parameters_max_textfield" :
		if (selected_group != null) {
		    selected_group.getChild("max").setValue(Float.parseFloat(event.getText()));
		}

		break;

	    case "parameters_mesh_textfield" :
		if (selected_group != null) {
		    selected_group.getChild("mesh").setValue(Integer.parseInt(event.getText()));
		}

		break;

	    case "parameters_world_name_textfield" :
		root.getChild("worldname").setValue(event.getText());

		break;

	    case "parameters_size_textfield" :
		root.getChild("size").setValue(Integer.parseInt(event.getText()));

		break;

	    case "parameters_seed_textfield" :
		root.getChild("seed").setValue(Long.parseLong(event.getText()));

		break;
	    }
	} catch (Exception e) {
	    Logger.getLogger(GenerationParametersScreen.class.getName()).log(Level.SEVERE,
			     "Textfield modification failed {0} {1}", new Object[] { id,
		    e.toString() });
	}
    }

    /**
     *
     * @param id
     * @param event
     * @throws Exception
     */
    @NiftyEventSubscriber(id = "parameters_load_parameters_button")
    public void loadParameters(String id, ButtonClickedEvent event) throws Exception {
	try {
	    String	filename	= save_filename_textfield.getDisplayedText();

	    Parameters.getParameterSet("general").parseDefinitionFile(filename);
	} catch (Exception e) {
	    Logger.getLogger(GenerationParametersScreen.class.getName()).log(Level.SEVERE,
			     "Couldnt load params to file {0}", save_filename_textfield.getDisplayedText());

	    throw e;
	}
    }

    /**
     * Event handler for when the save parameters button is clicked.
     * @param id
     * @param event
     */
    @NiftyEventSubscriber(id = "parameters_save_parameters_button")
    public void saveParameters(String id, ButtonClickedEvent event) {
	try {
	    String	filename	= save_filename_textfield.getDisplayedText();

	    Parameters.getParameterSet("general").save(filename);
	} catch (Exception e) {
	    Logger.getLogger(GenerationParametersScreen.class.getName()).log(Level.SEVERE,
			     "Couldnt save params to file {0}", save_filename_textfield.getDisplayedText());
	}
    }

    /**
     *
     * @param id
     * @param event
     * @throws Exception
     */
    @NiftyEventSubscriber(id = "parameters_value_listbox")
    public void onMyListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent<String> event)
	    throws Exception {
	selection	= event.getSelection().get(0);
	setSelectedGroup(selection);
	min_textfield.setText(String.valueOf(getSelectedMin()));
	max_textfield.setText(String.valueOf(getSelectedMax()));
	mesh_textfield.setText(String.valueOf(getSelectedMesh()));
    }

    /**
     *
     * @param nifty
     * @param screen
     */
    public void bind(Nifty nifty, Screen screen) {
	try {
	    this.nifty	= nifty;
	    this.screen	= screen;
	    root	= (ParameterGroup) Parameters.getParameterSet("general").getParameterNode("");
	    list_box	= screen.findNiftyControl("parameters_value_listbox", ListBox.class);
	    list_box.addItem("Elevation");
	    list_box.addItem("Temperature");
	    list_box.addItem("Rainfall");
	    list_box.addItem("Drainage");
	    list_box.addItem("Volcanism");
	    selection			= "";
	    min_textfield		= screen.findNiftyControl("parameters_min_textfield", TextField.class);
	    max_textfield		= screen.findNiftyControl("parameters_max_textfield", TextField.class);
	    mesh_textfield		= screen.findNiftyControl("parameters_mesh_textfield", TextField.class);
	    save_filename_textfield	= screen.findNiftyControl("parameters_save_filename_textfield",
		    TextField.class);
	    world_name_textfield	= screen.findNiftyControl("parameters_world_name_textfield", TextField.class);
	    size_textfield		= screen.findNiftyControl("parameters_size_textfield", TextField.class);
	    seed_textfield		= screen.findNiftyControl("parameters_seed_textfield", TextField.class);
	} catch (Exception ex) {
	    Logger.getLogger(GenerationParametersScreen.class.getName()).log(Level.SEVERE, null, ex);
	}
    }

    /**
     *
     * @param scr
     */
    public void goToScreen(String scr) {
	this.nifty.gotoScreen(scr);
    }

    /**
     *
     */
    public void onStartScreen() {
	try {
	    reset();
	} catch (Exception ex) {
	    Logger.getLogger(GenerationParametersScreen.class.getName()).log(Level.SEVERE, null, ex);
	}
    }

    /**
     *
     */
    public void onEndScreen() {}
}


//~ Formatted by Jindent --- http://www.jindent.com
